

def compress_rle8(pixels):
    """
    RLE8压缩，0表示原始内容（literal），1~254表示游程长度（run），255表示结束，数据为uint8_t。
    格式：
      - run: [run_len(1~254), value]
      - literal: [0, len(1~254), v1, v2, ...]
      - 255表示数据结束
    """
    n = len(pixels)
    result = []
    i = 0
    while i < n:
        run_val = pixels[i] & 0xFF
        run_len = 1
        while i + run_len < n and (pixels[i + run_len] & 0xFF) == run_val and run_len < 254:
            run_len += 1
        if run_len > 2:
            result.append(run_len)  # 1~254，表示游程长度
            result.append(run_val)
            i += run_len
        else:
            lit_start = i
            lit_vals = [pixels[lit_start] & 0xFF]
            i += 1
            while i < n and len(lit_vals) < 254:
                next_run_val = pixels[i] & 0xFF
                next_run_len = 1
                while i + next_run_len < n and (pixels[i + next_run_len] & 0xFF) == next_run_val and next_run_len < 254:
                    next_run_len += 1
                if next_run_len > 2:
                    break
                lit_vals.append(next_run_val)
                i += 1
            result.append(0)  # 0表示原始内容
            result.append(len(lit_vals))
            result.extend(lit_vals)
    result.append(255)  # 255表示结束
    return result
